/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import dao.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import modelo.Diploma;
import modelo.SituacaoDoDiploma;

/**
 *
 * @author Dirac
 */
public class SituacaoDoDiplomaJpaController {

    public SituacaoDoDiplomaJpaController() {
        emf = Persistence.createEntityManagerFactory("ArquivoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(SituacaoDoDiploma situacaoDoDiploma) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(situacaoDoDiploma);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(SituacaoDoDiploma situacaoDoDiploma) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SituacaoDoDiploma persistentSituacaoDoDiploma = em.find(SituacaoDoDiploma.class, situacaoDoDiploma.getId());
            Diploma diplomaOld = persistentSituacaoDoDiploma.getDiploma();
            Diploma diplomaNew = situacaoDoDiploma.getDiploma();
            if (diplomaNew != null) {
                diplomaNew = em.getReference(diplomaNew.getClass(), diplomaNew.getId());
                situacaoDoDiploma.setDiploma(diplomaNew);
            }
            situacaoDoDiploma = em.merge(situacaoDoDiploma);
            if (diplomaOld != null && !diplomaOld.equals(diplomaNew)) {
                diplomaOld.getSituacaoDoDiplomas().remove(situacaoDoDiploma);
                diplomaOld = em.merge(diplomaOld);
            }
            if (diplomaNew != null && !diplomaNew.equals(diplomaOld)) {
                diplomaNew.getSituacaoDoDiplomas().add(situacaoDoDiploma);
                diplomaNew = em.merge(diplomaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = situacaoDoDiploma.getId();
                if (findSituacaoDoDiploma(id) == null) {
                    throw new NonexistentEntityException("The situacaoDoDiploma with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SituacaoDoDiploma situacaoDoDiploma;
            try {
                situacaoDoDiploma = em.getReference(SituacaoDoDiploma.class, id);
                situacaoDoDiploma.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The situacaoDoDiploma with id " + id + " no longer exists.", enfe);
            }
            Diploma diploma = situacaoDoDiploma.getDiploma();
            if (diploma != null) {
                diploma.getSituacaoDoDiplomas().remove(situacaoDoDiploma);
                diploma = em.merge(diploma);
            }
            em.remove(situacaoDoDiploma);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<SituacaoDoDiploma> findSituacaoDoDiplomaEntities() {
        return findSituacaoDoDiplomaEntities(true, -1, -1);
    }

    public List<SituacaoDoDiploma> findSituacaoDoDiplomaEntities(int maxResults, int firstResult) {
        return findSituacaoDoDiplomaEntities(false, maxResults, firstResult);
    }

    private List<SituacaoDoDiploma> findSituacaoDoDiplomaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from SituacaoDoDiploma as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public SituacaoDoDiploma findSituacaoDoDiploma(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(SituacaoDoDiploma.class, id);
        } finally {
            em.close();
        }
    }

    public int getSituacaoDoDiplomaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from SituacaoDoDiploma as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public List<SituacaoDoDiploma> findToDiploma(Diploma diploma) {
         EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT t FROM SituacaoDoDiploma t WHERE t.diploma.id = :diploma ORDER BY t.dia DESC");
            q.setParameter("diploma", diploma.getId());

            return q.getResultList();
        } finally {
            em.close();
        }
    }

}
